'use strict';

angular.module('afasApp')
.filter('inWords', ['$filter', function ($filter) {
  return function (num) {

        var ones, scales, tens;

        ones = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"];

        tens = ["twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"];

        scales = ["", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion"];

        function convert(x) {
          var n, number, numberPart, previousScale, previousScaleValue, remainder, scaleValue, word, _i, _ref;
          number = Math.abs(x);
          if (number < 100) {
            return check_negative(x, convert_less_than_hundred(number));
          }
          if (number < 1000) {
            return check_negative(x, convert_less_than_thousand(number));
          }
          for (n = _i = 0, _ref = scales.length; 0 <= _ref ? _i <= _ref : _i >= _ref; n = 0 <= _ref ? ++_i : --_i) {
            previousScale = n - 1;
            scaleValue = Math.pow(1000, n);
            if (scaleValue > number) {
              previousScaleValue = Math.pow(1000, previousScale);
              numberPart = parseInt(number / previousScaleValue, 10);
              remainder = number - (numberPart * previousScaleValue);
              word = convert_less_than_thousand(numberPart) + " " + scales[previousScale];
              if (remainder > 0) {
                word = word + ", " + convert(remainder);
              }
              return check_negative(x, word);
            }
          }
        };

        function convert_less_than_hundred(number) {
          var i, tempVal, _i, _ref;
          if (number < 20) {
            return ones[number];
          }
          for (i = _i = 0, _ref = tens.length; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
            tempVal = 20 + 10 * i;
            if (tempVal + 10 > number) {
              if (number % 10 !== 0) {
                return tens[i] + " " + ones[number % 10];
              }
              return tens[i];
            }
          }
        };

        function convert_less_than_thousand(number) {
          var modulus, remainder, word;
          word = "";
          remainder = parseInt(number / 100, 10);
          modulus = parseInt(number % 100, 10);
          if (remainder > 0) {
            word = ones[remainder] + " hundred";
            if (modulus > 0) {
              word = word + " ";
            }
          }
          if (modulus > 0) {
            word = word + convert_less_than_hundred(modulus);
          }
          return word;
        };

        function check_negative(number, word) {
          if (number < 0) {
            return "negative " + word;
          } else {
            return word;
          }
        };

    return convert(num);
  };
}]);